//
//  RssParser.m
//  iBole
//
//  Created by Tao Chen on 12-3-12.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//
/*This file is part of iBole.
 
 iBole is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.
 
 iBole is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.
 
 You should have received a copy of the GNU General Public License
 along with Foobar.  If not, see <http://www.gnu.org/licenses/>.
 */
#import "RssParser.h"
#import "RssParseDelegate.h"
#import "Stack.h"

@implementation RssParser
{
    id<RssParseDelegate> delegate;
    Stack * xmlStateStack;
    NSString * curString;
}

@synthesize delegate;

-(id)init
{
    if(self = [super init])
    {
        xmlStateStack = [[Stack alloc] init];
        curString = nil;
    }
    return self;
}
-(void)dealloc
{
    [delegate release];
}
-(void)getRssFromNetwork
{
    NSURL * url = [[NSURL alloc] initWithString:BOLE_URL];
    NSXMLParser * parse = [[NSXMLParser alloc] initWithContentsOfURL:url];
    parse.delegate = self;
    if(![parse parse])
    {
        UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"出错了" message:@"获取RSS信息出错" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil];
        [alert show];
        [alert release];
    }
    [url release];
    [parse release];
}
-(bool)shouldParse
{
      return [self isTitle] || [self isDetail];
}
-(bool)isTitle
{
    if ( [xmlStateStack hasObject:@"item"]) {
        if ([[xmlStateStack top] isEqualToString:@"title"]) {
            return YES;
        }
    }
    return NO;
}
-(bool)isDetail
{
    if ( [xmlStateStack hasObject:@"item"]) {
      if([[xmlStateStack top] isEqualToString:@"description"]){
            return YES;
        }
    }
    return NO;
}

- (void)parser:(NSXMLParser *)parser
didStartElement:(NSString *)elementName
  namespaceURI:(NSString *)namespaceURI
 qualifiedName:(NSString *)qName
    attributes:(NSDictionary *)attributeDict
{
    [xmlStateStack push:elementName];
//    NSLog(@"starting : %@", elementName);
    if (self.delegate && [self isTitle]) {
        [self.delegate itemBegin];
    }
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName;
{
    if ([self isTitle]) {
         [self.delegate itemTitle:curString];
    }
    if ([self isDetail]) {
         [self.delegate itemDetail:curString];
    }
    if (curString) {
        [curString release];
        curString = nil;
    }
    [[xmlStateStack pop] isEqualToString:elementName];
//    NSLog(@"ending : %@", elementName);
}
- (void)parser:(NSXMLParser *)parser
foundCharacters:(NSString *)string
{
//        NSLog(@" Element: %@", string);
        if ([self shouldParse]) {
            if (!curString) {
                curString = [[NSString alloc] initWithString:string];
            } else {
                NSString * tmp = [curString stringByAppendingString:string];
                [curString release];
                curString = [[NSString alloc] initWithString:tmp];
            }
    }
}

@end
